//
//  DelayCell.swift
//  DSP
//
//  Created by tucici on 2024/5/15.
//

import UIKit
import TCCKit
protocol DelayCellDelegate {
    func select(ch:ChannelModel, index:IndexPath)
}

class DelayCell: UITableViewCell {
    private var line : UIView!
    private var title : UILabel!
    private var collectionView : UICollectionView!
    private var selectCH : ChannelModel = ChannelModel()
    var delegate : DelayCellDelegate?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        backgroundColor = .clear
        selectionStyle = .none
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        title = UILabel()
        title.text = "Time Delay"
        title.textHexColor = "#000000"
        title.bgHexColor = "#C0C0C0"
        title.font = TCCKit.font(18.0,.medium)
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .vertical
        flowlayout.minimumLineSpacing = 10.0.k_h
        flowlayout.minimumInteritemSpacing = 10.0.k_w
        flowlayout.sectionInset = UIEdgeInsets(top: k_h(4), left: k_w(10), bottom: k_w(6), right: k_h(10))
        flowlayout.footerReferenceSize = CGSize(width: kUIScreenWidth, height: k_h(40))
        collectionView = UICollectionView(frame:.zero,collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(DelayItem.self)
        collectionView.register(DelayFooter.self, .footer)
        
        contentView.addSubview(line)
        contentView.addSubview(title)
        contentView.addSubview(collectionView)
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.centerY.equalTo(title.snp.centerY)
        }
        
        title.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(title.snp.bottom)
            make.height.equalTo(0)
        }
    }
    
    
    var array = [ChannelModel](){
        didSet{
            guard let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else{return}
            let w = itemWidth(flowlayout, array.count)
            flowlayout.itemSize = CGSize(width: w, height: k_h(41))
            let h = collectionH(flowlayout, array, array.count)
            collectionView.reloadData()
            collectionView.snp.updateConstraints { make in
                make.height.equalTo(h)
            }
        }
    }
    
    func sendData(_ model:ChannelModel){
        _ = BLEData.delay(model).write
    }
}

//MARK: - UICollectionViewDataSource
extension DelayCell:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cell(DelayItem.self, indexPath) as! DelayItem
        cell.model = array[indexPath.row]
        cell.block = {[weak self] model in
            self?.sendData(model)
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.reusableView(DelayFooter.self, .footer, indexPath) as! DelayFooter
        view.model = selectCH
        view.block = {[weak self] model in
            self?.sendData(model)
        }
        return view
    }
}
//MARK: - UICollectionViewDelegate
extension DelayCell:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        let model = array[indexPath.row]
        if model.select || model.enable == false{return}
        selectCH = array[indexPath.row]
        delegate?.select(ch: selectCH, index: indexPath)
    }
}


